package app.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;

public class PagerLayout extends LinearLayout
{
    RecyclerView vList;

    public PagerLayout(Context context){
        super(context);
        init(context, null);
    }

    public PagerLayout(Context context, AttributeSet attrs){
        super(context, attrs);
        init(context, attrs);
    }

    public PagerLayout(Context context, AttributeSet attrs, int defStyleAttr){
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    void init(Context context, AttributeSet attrs){

    }

    @SuppressLint("WrongConstant")
    @Override
    protected void onFinishInflate(){
        super.onFinishInflate();
        if(vList == null){
            vList = new RecyclerView(getContext());
            vList.setLayoutManager(new LinearLayoutManager(getContext(), getOrientation(), false));
            new PagerSnapHelper().attachToRecyclerView(vList);
            addView(vList);
        }
    }

    @Override
    public void onViewAdded(View child){
        super.onViewAdded(child);
        vList = null;
        for(int i = 0; i < getChildCount(); i++){
            View view = getChildAt(i);
            if(vList == null && view instanceof RecyclerView){
                vList = (RecyclerView)view;
                break;
            }
        }
        if(vList == null) return;

        ViewGroup group = null;
        for(int i = 0; i < getChildCount(); i++){
            View view = getChildAt(i);
            if(view == vList) continue;
            if(group == null && view instanceof ViewGroup){
                group = (ViewGroup)view;
            }
            removeViewAt(i--);
        }
        if(group == null) return;

        List<View> views = new ArrayList<>();
        for(int i = 0; i < group.getChildCount(); i++) views.add(group.getChildAt(i));
        group.removeAllViews();

        LocalAdapter adapter = null;
        if(vList.getAdapter() instanceof LocalAdapter){
            adapter = (LocalAdapter)vList.getAdapter();
        }else{
            adapter = new LocalAdapter();
        }
        adapter.list = views;
        vList.setAdapter(adapter);
        int index = getBaselineAlignedChildIndex();
        if(index >= 0 && index < adapter.getItemCount()) vList.scrollToPosition(index);
        setBaselineAlignedChildIndex(0);
    }

    class LocalAdapter extends Adapter
    {
        List<View> list;

        @Override
        public int getItemCount(){
            return list == null? 0: list.size();
        }

        @Override
        public int getItemViewType(int p){
            return p;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup pa, int type){
            return new ViewHolder(list.get(type)){};
        }

        @Override
        public void onBindViewHolder(ViewHolder h, int p){

        }
    }

}
